package com.hsq.commons.util.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SqlParam {

			//private List<String> criteriaWithoutValue;
			private List<Map<String, Object>> criteriaWithSingleValue;
			private List<Map<String, Object>> criteriaWithListValue;
			private List<Map<String, Object>> criteriaWithBetweenValue;
			private List<Map<String, Object>> criteriaWithSql;
			
			private List<Map<String, Object>> updateListValue;
			
			private List<Map<String, Object>> orderListValue;
			

			public List<Map<String, Object>> getUpdateValueList() {
				return updateListValue;
			}

			private int currentPage = 1;
			private int pageSize=0;

			public int getPageSize() {
				return pageSize;
			}

			public void setPageSize(int pageSize) {
				this.pageSize = pageSize;
			}

			public int getCurrentPage() {
				return currentPage<=0?1:currentPage;
			}

			public void setCurrentPage(int currentPage) {
				this.currentPage = currentPage;
			}

			public  SqlParam() {
			    //criteriaWithoutValue = new ArrayList<String>();
			    criteriaWithSingleValue = new ArrayList<Map<String, Object>>();
			    criteriaWithListValue = new ArrayList<Map<String, Object>>();
			    criteriaWithBetweenValue = new ArrayList<Map<String, Object>>();
			    updateListValue = new ArrayList<Map<String, Object>>();
			    
			    
			    criteriaWithSql  = new ArrayList<Map<String, Object>>();
			    
			    orderListValue = new ArrayList<Map<String, Object>>();
			}

//			public List<String> getCriteriaWithoutValue() {
//			    return criteriaWithoutValue;
//			}
			
			public List<Map<String, Object>> getCriteriaWithSql() {
			    return criteriaWithSql;
			}
			
			public List<Map<String, Object>> getOrderListValue() {
			    return orderListValue;
			}

			public List<Map<String, Object>> getCriteriaWithSingleValue() {
			    return criteriaWithSingleValue;
			}

			public List<Map<String, Object>> getCriteriaWithListValue() {
			    return criteriaWithListValue;
			}

			public List<Map<String, Object>> getCriteriaWithBetweenValue() {
			    return criteriaWithBetweenValue;
			}
			
			private void addUpdateListValue(String condition, Object value) {
			    if (value == null) {
				throw new RuntimeException("Value for " + condition	+ " cannot be null");
			    }
			    Map<String, Object> map = new HashMap<String, Object>();
			    map.put("condition", condition);
			    map.put("value", value);
			    updateListValue.add(map);
			}
			
			private void addOrderListValue(String condition, Object value) {
			    if (value == null) {
				throw new RuntimeException("order by  " + condition	+ " cannot be null");
			    }
			    Map<String, Object> map = new HashMap<String, Object>();
			    map.put("condition", condition);
			    map.put("value", value);
			    orderListValue.add(map);
			}

			private void addCriterion(String condition, Object value) {
			    if (value == null) {
			    	throw new RuntimeException("Value for " + condition	+ " cannot be null");
			    }
			    Map<String, Object> map = new HashMap<String, Object>();
			    map.put("condition", condition);
			    map.put("value", value);
			    criteriaWithSingleValue.add(map);
			}
			
			private void addCriterion(String sql) {
			    if (sql == null) {
			    	throw new RuntimeException("Value for " + sql	+ " cannot be null");
			    }
			    Map<String, Object> map = new HashMap<String, Object>();
			    map.put("condition", sql);
			    criteriaWithSql.add(map);
			}

			private void addCriterion(String condition, List<? extends Object> values) {
			    if (values == null || values.size() == 0) {
				throw new RuntimeException("Value list for " + condition
					+ " cannot be null or empty");
			    }
			    Map<String, Object> map = new HashMap<String, Object>();
			    map.put("condition", condition);
			    map.put("values", values);
			    criteriaWithListValue.add(map);
			}

			private void addCriterion(String condition, Object value1,Object value2) {
			    if (value1 == null || value2 == null) {
				throw new RuntimeException("Between values for " + condition
					+ " cannot be null");
			    }
			    List<Object> list = new ArrayList<Object>();
			    list.add(value1);
			    list.add(value2);
			    Map<String, Object> map = new HashMap<String, Object>();
			    map.put("condition", condition);
			    map.put("values", list);
			    criteriaWithBetweenValue.add(map);
			}

	
			public SqlParam andSQL(String sql) {
			    addCriterion(sql);
			    return this;
			}
			
			public SqlParam andEqualTo(String field,String value) {
			    addCriterion(field+" =", value);
			    return this;
			}
			
			public SqlParam andEqualTo(String field,Integer value) {
			    addCriterion(field+" =", value);
			    return this;
			}
			
		

			public SqlParam andNotEqualTo(String field,String value) {
			    addCriterion(field+" <>", value);
			    return this;
			}

			public SqlParam andGreaterThan(String field,String value) {
			    addCriterion(field+" >", value);
			    return this;
			}

			public SqlParam andGreaterThanOrEqualTo(String field,String value) {
			    addCriterion(field+" >=", value);
			    return this;
			}

			public SqlParam andLessThan(String field,String value) {
			    addCriterion(field+" <", value);
			    return this;
			}

			public SqlParam andLessThanOrEqualTo(String field,String value) {
			    addCriterion(field+" <=", value);
			    return this;
			}

			public SqlParam andLike(String field,String value) {
			    addCriterion(field+" like ", value);
			    return this;
			}

			public SqlParam andNotLike(String field,String value) {
			    addCriterion(field+" not like", value);
			    return this;
			}

			public SqlParam andIn(String field,List<String> values) {
			    addCriterion(field+" in", values);
			    return this;
			}

			public SqlParam andNotIn(String field,List<String> values) {
			    addCriterion(field+" not in", values);
			    return this;
			}

			public SqlParam andBetween(String field,String value1, String value2) {
			    addCriterion(field+" between", value1, value2);
			    return this;
			}

			public SqlParam andNotBetween(String field,String value1, String value2) {
			    addCriterion(field+" not between", value1, value2);
			    return this;
			}
			
			public SqlParam andUpdateListValue(String field,String value) {
				addUpdateListValue(field+" = ", value);
			    return this;
			}
			
			public SqlParam andOrderListValue(String field,String value) {
				addOrderListValue(" order by " +field+" ", value);
			    return this;
			}
}
